package com.baiyang.server.service.read.impl;


import com.baiyang.server.model.article.Article;
import com.baiyang.server.model.article.ArticleTypeRel;
import com.baiyang.server.model.base.ArticleType;
import com.baiyang.server.model.read.ReadRecord;
import com.baiyang.server.service.article.ArticleService;
import com.baiyang.server.service.article.ArticleTypeRelService;
import com.baiyang.server.service.base.ArticleTypeService;
import com.baiyang.server.service.impl.BaseServiceImpl;
import com.baiyang.server.service.read.ReadRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baiyang.server.dao.read.BillDao;
import com.baiyang.server.model.read.Bill;
import com.baiyang.server.service.read.BillService;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author XVX
 * BY: @Primary 注解帮助生命@Autowired优先注入的实列
 */
@Service	
public class BillServiceImpl extends BaseServiceImpl<BillDao, Bill> implements BillService{
    @Autowired
    private BillDao billDao = null;
    @Autowired
    private ArticleService articleService = null;
    @Autowired
    private ArticleTypeRelService articleTypeRelService;
    @Autowired
    private ArticleTypeService articleTypeService = null;
    @Autowired
    private ReadRecordService readRecordService = null;
    @Override
    public List<Bill> getUserBill(String userId, int pageStart, int pageSize) {
        List<Bill> bills = billDao.getUserBill(userId,pageStart,pageSize);
        bills.forEach(e->{
            if(e.getType().equals("0")){
                Article article = new Article();
                ReadRecord readRecord = new ReadRecord();
                readRecord.setId(e.getRelationId());
                readRecord = readRecordService.selectOne(readRecord);
                article.setId(readRecord.getArticleId());
                article = articleService.selectOne(article);
                if(article != null){
                    ArticleTypeRel rel = new ArticleTypeRel();
                    rel.setArticleId(article.getId());
                    List<ArticleTypeRel> rels = articleTypeRelService.selectList(rel);
                    List<String> ids = new ArrayList<>();
                    rels.forEach(r->{
                        ids.add(r.getTypeId());
                    });
                    List<ArticleType> articleTypes = articleTypeService.selectByIds(ids);
                    article.setArticleTypes(articleTypes);
                    articleService.setDetail(article);
                    e.setArticle(article);
                }
            }
        });
        return bills;
    }
}
